package com.drx.service.service;

import com.drx.dao.TabUserMapper;
import com.drx.dojo.TabUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TabUsersServiceImpl implements com.drx.service.TabUsersService {
    @Autowired
    TabUserMapper tabUserMapper;

    @Override
    public Integer addUsers(String[] userNames) {
        Integer result=0;
        //首先遍历usernames数组，通过名字创建users对象
        for (String userName:userNames){
            TabUser tabUser=new TabUser();
            tabUser.setStudentnum(Integer.valueOf(userName));
            //初始密码同意设置为123456
            tabUser.setPassword("123456");
            //判断当前用户名是否已经存在
            TabUser tabUser1 = tabUserMapper.selectUserByStudentNum(Integer.valueOf(userName));
            if (tabUser1==null){
                //调用mapper方法插入数据
                result+=tabUserMapper.insert(tabUser);
            }

        }

        return result;
    }

    @Override
    public List<Integer> selectAllUserNames() {
        List<Integer> userNames = tabUserMapper.selectAllUserNames();
        return userNames;
    }

    @Override
    public Integer deleteUsers(String[] userNames) {
        Integer result=0;
        //遍历userNames数组，获取到要删除的用户名
        for(String userName:userNames){
            //判断当前用户名是否已经存在
            TabUser tabUser1 = tabUserMapper.selectUserByStudentNum(Integer.valueOf(userName));
            if (tabUser1!=null){
                //元素存在的情况下删除元素
                result+=tabUserMapper.deleteByStudentNum(Integer.valueOf(userName));
            }
        }
        return result;
    }

    @Override
    public Integer changePassword(Integer studentNum, String password) {
        //先查询是否有该用户
        TabUser tabUser = tabUserMapper.selectUserByStudentNum(studentNum);
        if (tabUser!=null){
            tabUser.setPassword(password);
            int i = tabUserMapper.updateByPrimaryKey(tabUser);
            return i;
        }else{
            return 0;
        }
    }
}
